class Preload {
  constructor() {
    // this.domainStr = "//n.sinaimg.cn/sports/caitong/gxfc/game/static"
    let domain = "//n.sinaimg.cn/sports/caitong/gxfc/dogWang/img/lantern/web/"
    // let domain = "/src/static/image/lantern/web/"
    this.domain=domain;
    this.version="t01"
    this.imgArr =[ 'bg_playing.png', 'bg_preload.png', 'bg_preload1.png', 'btn_num.png', 'btn_start.png', 'btn_time.png', 'cloud_bottom.png', 'cloud_top.png', 'player_man.png', 'player_pop.png', 'word_rules.png','bg_txt.png','player_restart.png']
    // this.imgNames = [
    //   "bg_index","btn_start","btn_arrow","word_wang","heart","bg_heart","pointer","bg_playing","bg_over",
    // ];
    this.imgNames=null;
  }
  initRender() {
    this.bh = game.world.height;
    this.bw = game.world.width;
    this.cx = game.world.centerX;
    this.cy = game.world.centerY;
  }
  scaleFlag(){
    return this.bw/375;
  }
  scales(obj,flag=0.5){
    let num=this.scaleFlag()*flag;
    obj.scale.set(num);
  }
  setX(num){
    return this.bh*(num/600);
  }
  setY(num){
    return this.bw*(num/375);
  }
  preload() {
    this.initRender();
    game.stage.backgroundColor = "#CD0014";
  }
  setPreWord(str = "0%") {
    if (str == "0%") {
      this.preWord = game.add.text(game.world.centerX, game.world.centerY, str, {
        fill: "#fff",
        fontSize: "40px"
      });
    }
    this.preWord.left = this.cx - (this.preWord.width / 2);
    this.preWord.top = this.cy + 80;
    this.preWord.text = str;
    // console.log(this.preWord.text);
  }
  initArrs(){
    this.imgArr=this.imgArr.map(v=>{
      return `${this.domain}${v}?v=${this.version}`
    });
    this.imgNames=this.imgArr.map(v=>{
       let str=v.match(/\w\/(\w+)\./g);
       return  RegExp.$1      
    })
  }
  create() {
    this.initArrs();
    game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;//不设置show_all的话引擎会定时适应，无法自己设置场景宽高。
    let gameBox=document.querySelector("#game canvas");
    gameBox.style.width=window.innerWidth+"px";
    gameBox.style.height=window.innerHeight+"px";
    this.setPreWord();
    game.load.crossOrigin = 'anonymous';
    game.load.images(this.imgNames, this.imgArr);
    game.load.atlas("circles",`${this.domain}circles.png?v=${this.version}`,`${this.domain}circles.js?v=${this.version}`);
    game.load.start();
    let self = this;
    game.load.onFileComplete.add(function (progress) {
      self.setPreWord(progress + "%");
    });
    game.load.onLoadComplete.add(function () {
      game.state.start("Start")
      // let test=game.add.sprite(0,0,"dices","step4.png")
      // test.frame=2;
    });
  }
}
module.exports = Preload;
